import { ShellConfig } from "@/config";
import { BrowserWindow } from "electron";


/** @type {*} */
const windowMap: Map<string, number> = new Map();

/**
 * @description
 * @export
 * @param {*} winConfig 窗口参数
 * @param {string} path 窗口路径
 */
export function createWindow(winConfig: any, path: string) {
  // 创建窗口
  const win = new BrowserWindow(
    Object.assign(
      {
        webPreferences: {
          // 用vue.config.js的nodeIntegration,启用后渲染器中可以使用nodejs API
          // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
          nodeIntegration: process.env
            .ELECTRON_NODE_INTEGRATION as unknown as boolean,
          contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
        },
      },
      winConfig
    )
  );
  win.loadURL(path);
  windowMap.set(winConfig.tag, win.id);
  win.addListener('close', (event: any) => {
    if(!win.isFocused() || !ShellConfig.isNoQuit){
        destroyedWindow(event, winConfig.tag);
    }else{
        event.preventDefault();  //阻止窗口的关闭事件
        win.hide();
    }
  })
  return win;
}

/**
 * 窗口关闭后销毁窗口对象
 *
 * @param {*} event
 * @return {*} 
 */
function destroyedWindow(event: any,tag: string) {
  if (!event.sender.id || !tag) {
    return;
  }
  windowMap.delete(tag);
}

/**
 * 修改窗口大小
 *
 * @export
 * @param {number} width 宽度
 * @param {number} height 高度
 * @param {boolean} [animate] 动画
 * @param {string} [tag] 窗口tag
 * @return {*} 
 */
export function updateWindowSize(width: number, height: number, animate?: boolean, winID?: number) {
  //  不传则为主窗口 主窗口id为1
  if (!winID) {
    winID = 1;
  }
  const win: BrowserWindow | null = BrowserWindow.fromId(winID);
  if (!win) {
    console.log("窗口不存在");
    return;
  }
  win.setContentSize(width, height, animate);
}

/**
 * @description 最小化窗口
 * @export
 * @param winID 窗口id
 */
export function minWindow(winID?: number) {
  //  不传则为主窗口 主窗口id为1
  if (!winID) {
    winID = 1;
  }  
  const win: BrowserWindow | null = BrowserWindow.fromId(winID);
  if (win) {
    win.unmaximize();
  }
}

/**
 * @description 最大化窗口
 * @export
 * @param winID 窗口id
 */
export function maxWindow(winID?: number) {
  //  不传则为主窗口 主窗口id为1
  if (!winID) {
    winID = 1;
  }  
  const win: BrowserWindow | null = BrowserWindow.fromId(winID);
  if (win) {
    win.maximize();
  }
}

/**
 * @description 自动判断最大or最小化窗口
 * @export
 * @param winID 窗口id
 */
export function maxOrMinWindow(winID?: number) {
  //  不传则为主窗口 主窗口id为1
  if (!winID) {
    winID = 1;
  }
  const win: BrowserWindow | null = BrowserWindow.fromId(winID);
  if (win && win.isMaximized()) {
    win.unmaximize();
  } else if (win && !win.isMaximized()) {
    win.maximize();
  }
}

/**
 * @description 缩小窗口到任务栏
 * @export
 * @param winID 窗口id
 */
export function shrinkWindow(winID?: number){
  //  不传则为主窗口 主窗口id为1
  if (!winID) {
    winID = 1;
  }  
  const win: BrowserWindow | null = BrowserWindow.fromId(winID);
  if (win) {
    win.minimize();
  }
}

export { windowMap }